home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Hot Mix 17
/
Hot Mix 17.iso
/
HM17_SGI
/
research
/
lib
/
factorial.pro
< prev
next >
Wrap
Text File
|
1997-07-08
|
2KB
|
67 lines
;$Id: factorial.pro,v 1.3 1997/01/15 03:11:50 ali Exp $
;
; Copyright (c) 1994-1997, Research Systems, Inc. All rights reserved.
; Unauthorized reproduction prohibited.
;+
; NAME:
; FACTORIAL
;
; PURPOSE:
; This function computes the factorial N! as the double-precision
; product, (N) * (N-1) * (N-2) * ...... * 3 * 2 * 1.
;
; CATEGORY:
; Special Functions.
;
; CALLING SEQUENCE:
; Result = Factorial(n)
;
; INPUTS:
; N: A non-negative scalar of type integer, float or double.
;
; KEYWORD PARAMETERS:
; STIRLING: If set to a non-zero value, Stirling's asymptotic
; formula is used to approximate N!.
;
; EXAMPLE:
; Compute 20! with and without Stirling's asymptotic formula.
; result_1 = factorial(20, /stirling)
; result_2 = factorial(20)
;
; Result_1 and result_2 should be 2.4227869e+18 and 2.4329020e+18
; respectively.
;
; REFERENCE:
; ADVANCED ENGINEERING MATHEMATICS (seventh edition)
; Erwin Kreyszig
; ISBN 0-471-55380-8
;
; MODIFICATION HISTORY:
; Written by: GGS, RSI, November 1994
;-
function factorial, n, stirling = stirling
;Computes N! as (N) * (N-1) * (N-2) * ...... * 3 * 2 * 1
;Test example: 20! = 2.4329020e+18
;Use NR_MACHAR(/DOUBLE) to determine largest floating point number.
;Stirling's formula: N! = SQRT(2.0d*!PI*N) * (N/EXP(1.0d))^(N+0.0d)
on_error, 2
if n lt 0 then $
message, 'n must be a non-negative scalar.'
;Computes N! as (N) * (N-1) * (N-2) * ...... * 3 * 2 * 1
if keyword_set(stirling) eq 0 then begin
fact = 1.0d
for k = n+0.0d, 1d, -1d do begin
fact = fact * k
endfor
endif else $ ;Approximate N! using Stirling's formula.
fact = sqrt(2.0d * !pi * n) * (n / exp(1.0d))^(n+0.0d)
return, fact
end